package it.intecs.pisa.smarcos.activities;

import it.intecs.pisa.smarcos.R;
import it.intecs.pisa.smarcos.base.BeepManager;
import it.intecs.pisa.smarcos.base.SpeechManager;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Compass extends BaseActivity
{
	public static final String INTENT_SHOW="it.intecs.pisa.smarcos.activities.Compass.SHOW";
	private static final String TAG = Compass.class.getSimpleName();
	
	private View view; 
	private TextView textView;
	private ViewGroup baseCenterView;
	private String position;
	private String lastInformation;
	private String actualDirection = "Empty";
	private Vibrator vibrator;
	private BeepManager beepManager;
	private SpeechManager speechManager;
    private SensorManager sensorManager = null;
    private OrientationListener ol;
    private float bAzimuth = 900;
    private float azimuth;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)    {
        super.onCreate(savedInstanceState);
        
        Log.d(TAG, "OnCreate");

		speechManager = SpeechManager.getInstance(getApplicationContext());
		beepManager = BeepManager.getInstance(getApplicationContext());
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

		// Metodo di BaseViewManager
		Init(getBaseContext());
		inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		view = inflater.inflate(R.layout.generic_option, null);
		setContentView(getBaseView());
		baseCenterView = (ViewGroup) findViewById(R.id.center_base_view);
		baseCenterView.addView(view);	
		
		textView = (TextView) findViewById(R.id.Option);
		
		position = getString(R.string.TTS_Compass);
		
		// Orientation
        ol = new OrientationListener();
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(ol, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
    }
    
    @Override
    public void onDestroy(){
    	sensorManager.unregisterListener(ol);
    	//locationManager.removeUpdates(locationListener);
    	super.onDestroy();
    }
    
	// CLOSE
	@Override
	public void onClick_A(){
		Log.d(TAG, "onClick_A");
			
		Intent intent;
		intent=new Intent("it.intecs.pisa.smarcos.services.ResultReceiver.SHOW");
		intent.putExtra("Result", "NORESULT");
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		sendBroadcast(intent);
		sentResult = true;
		finish();	
	}
	
	// POSITION
	@Override
	public void onClick_B(){
		Log.d(TAG, "onClick_B");
		
		speechManager.say(position, true, true);
	}
	
	// REPEAT 
	@Override
	public void onClick_C(){
		Log.d(TAG, "onClick_C");
		speechManager.say(lastInformation, true, true);
	}

	// BACK - DONE
	@Override
	public void onClick_D(){

		Log.d(TAG, "onClick_D");
		onClick_A();
	}
		
	private class OrientationListener implements SensorEventListener {

		private final String TAG = OrientationListener.class.getSimpleName();

		public void onSensorChanged(SensorEvent event) {

			//Azimuth, Pitch, Roll
			azimuth = event.values[0];
			//float pitch = event.values[1];
			//float roll = event.values[2];
				
			if (Math.abs(bAzimuth - azimuth) > 5.0){
			
				Log.d(TAG, "Azimuth " + azimuth);	
				
				bAzimuth = azimuth;

				computeDirection();

			}
		}

		public void onAccuracyChanged(Sensor arg0, int arg1){}
	}

	private boolean computeDirection(){

		String direction = "";
		
		if (((azimuth >= 0) && (azimuth < 20)) || ((azimuth >= 340) && (azimuth < 360)))
			direction = "Nord";
		if (((azimuth >= 20) && (azimuth < 80)))
			direction = "Nord Est";
		if (((azimuth >= 80) && (azimuth < 110)))
			direction = "Est";
		if (((azimuth >= 110) && (azimuth < 160)))
			direction = "Sud Est";
		if (((azimuth >= 160) && (azimuth < 200)))
			direction = "Sud";
		if (((azimuth >= 200) && (azimuth < 250)))
			direction = "Sud Ovest";
		if (((azimuth >= 250) && (azimuth < 290)))
			direction = "Ovest";
		if (((azimuth >= 290) && (azimuth < 340)))
			direction = "Nord Ovest";
		
		if (!actualDirection.equals(direction)) {
			actualDirection = direction;
			lastInformation = direction;
			textView.setText(lastInformation);
			speechManager.say(actualDirection, true, false);
			return true;
		}	
		
		return false;
	}
}

